<template>
  <div id="container" style="font-color:white">
    <!-- <img class="logo" src="/images/logoo.jpg" /> -->
    <h1 style=" font-size:30px;margin-left:30px">优坊后台管理系统</h1>
    <div style="font-weight:bold" class="title">欢迎你,{{username}}</div>
    <el-dropdown class="dropdown" @command="handleCommand">
      <span class="el-dropdown-link">
        <b style="color:white;font-size:17px">系统设置</b>
        <i class="el-icon-arrow-down el-icon--right" style="color:white"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="message">管理员信息</el-dropdown-item>
        <el-dropdown-item command="outlogin">注销</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <el-dialog class="info" title="管理员信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="user" ref="userForm" label-width="100px">
        <el-form-item label="账号：" prop="username" style="text-align:left">{{user.username}}</el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :rules="[{ required: true, message: '密码不可为空', trigger: 'blur' }]"
        >
          <el-input type="password" v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="password"
          :rules="[{ required: true, message: '确认密码不可为空', trigger: 'blur' }]"
        >
          <el-input type="password" v-model="confirmPassword"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">密码重置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { clearToken } from "@/utils/localStorage.js";
import { getByName, updateByUsername } from "@/api/admin.js";
export default {
  name: "",
  data() {
    return {
      user: {},
      username: "",
      confirmPassword: "",
      dialogVisible: false,
    };
  },
  created() {
    this.username = localStorage.username;
  },
  methods: {
    handleCommand(command) {
      if (command == "message") {
        this.dialogVisible = true;
        this.getUserByUsername();
      } else if (command == "outlogin") {
        clearToken();
        this.$router.push("/");
      }
    },
    resetPassword() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          if (this.user.password != this.confirmPassword) {
            alert("密码不一致！");
            return;
          }
          this.user.updateDate=null
          updateByUsername(this.user).then((res) => {
            if (res.data.code == "0000") {
              this.$message("密码重置成功");
              clearToken();
              this.$router.push("/");
            }
          });
          this.dialogVisible = false;
        } else {
          return false;
        }
      });
    },
    getUserByUsername() {
      getByName(this.username).then((res) => {
        if (res.data.code == "0000") {
          this.user = res.data.data;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
#container {
  display: flex;
  background-color: rgb(0, 128, 128);
  height: 100px;
  margin: 0;
  padding: 0;
  align-items: center;
  position: relative;
}
h1 {
  color: white;
}
.dropdown {
  position: absolute;
  right: 20px;
}
.title {
  position: absolute;
  left: 25%;
}
</style>